package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.drp.entity.Cargo;
import com.dy.drp.service.ICargoService;
import com.dy.drp.util.SelectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/cargo")
@Api(tags = "货物操作接口")
public class CargoController {
    @Autowired
    private ICargoService cargoService;

    @PostMapping("/cargoImg")
    @ApiOperation("添加货物图片")
    public String cargoImg(@RequestParam("cargoImg") MultipartFile file)  throws IOException{
        String fileSrc = file.getOriginalFilename(); //获取传过来的文件的文件名
        //判断文件后缀合法
        String[] filename = fileSrc.split("\\.");
        if (!filename[filename.length-1].equals("jpg")){
            return null;
        }
        //文件重命名
        fileSrc = System.currentTimeMillis() + fileSrc;
        //判断系统是否存在存储文件夹
        File file1 = new File("f:"+File.separator+"img");
        if (!file1.exists()){
            file1.createNewFile();
        }
        //文件放在本地的位置
        File f = new File("f:"+File.separator+"img"+File.separator+fileSrc);
        file.transferTo(f);

        return "/img/"+fileSrc;
    }

    @GetMapping("/selectOnePageCargo/{pageNum}/{pageSize}")
    @ApiOperation("查询单页货物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面展示信息条数")
    })
    public Page<Cargo> selectOnePage(@PathVariable("pageNum")Integer pageNum,
                                     @PathVariable("pageSize")Integer pageSize){
        Page<Cargo> page = new Page<>(pageNum,pageSize);
        return cargoService.page(page, null);
    }

    @PostMapping("/selectOnePageByInfo")
    @ApiOperation("添加出库商品 -- 查询采购信息")
    public Page<Cargo> selectOnePageByInfo(SelectUtil selectUtil){
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.like("cargo_name",selectUtil.getName());
        wrapper.like("cargo_type",selectUtil.getStartTime());
        Page<Cargo> page = new Page<>(selectUtil.getPageNum(),selectUtil.getPageSize());
        return cargoService.page(page,wrapper);
    }

    @GetMapping("/selectOneCargoinfo/{cargoName}/{cargoFactory}")
    @ApiOperation("单个商品信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cargoName",value="货物名称"),
            @ApiImplicitParam(name="cargoFactory",value="厂家")
    })
    public Cargo selectOneCargoinfo(@PathVariable("cargoName")String cargoName,
                                    @PathVariable("cargoFactory")String cargoFactory){
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_name",cargoName);
        wrapper.eq("cargo_factory",cargoFactory);
        return cargoService.getOne(wrapper);
    }

    @PostMapping("/updateCargoSaleMoney")
    @ApiOperation("修改销售价")
    public String updateCargoSaleMoney(Cargo cargo){
        QueryWrapper<Cargo> wrapper = new QueryWrapper<>();
        wrapper.eq("cargo_name",cargo.getCargoName());
        wrapper.eq("cargo_factory",cargo.getCargoFactory());
        if (cargoService.update(cargo,wrapper)){
            return "修改成功";
        }
        return "修改失败";
    }
}
